import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  EventEmitter, Host,
  Input, Optional,
  Output, TemplateRef, ViewEncapsulation,
} from '@angular/core';
import { ApesNoAnimationDirective, InputBoolean, zoomBigMotion } from '@shared/apes/core';

import { ApesToolTipBaseComponentLegacy, ApesToolTipTrigger, ApesToolTipComponent } from '@shared/apes/tooltip';

@Component({
  selector           : 'apes-popconfirm',
  exportAs: 'apesPopconfirmComponent',
  preserveWhitespaces: false,
  animations         : [ zoomBigMotion ],
  templateUrl        : './apes-popconfirm.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  encapsulation: ViewEncapsulation.None,
  providers: [
    {
      provide: ApesToolTipBaseComponentLegacy,
      useExisting: ApesPopConfirmComponent
    }
  ],
  styles             : [ `
    .apes-popover {
      position: relative;
    }
  ` ]
})
export class ApesPopConfirmComponent extends ApesToolTipComponent {
  @Input() apesOkText: string;
  @Input() apesOkType: string = 'primary';
  @Input() apesCancelText: string;
  @Input() @InputBoolean() apesCondition = false;
  @Input() apesIcon: string | TemplateRef<void>;

  @Output() readonly apesOnCancel: EventEmitter<void> = new EventEmitter();
  @Output() readonly apesOnConfirm: EventEmitter<void> = new EventEmitter();

  _prefix = 'apes-popover-placement';
  _trigger: ApesToolTipTrigger = 'click';
  _hasBackdrop = true;

  constructor(cdr: ChangeDetectorRef, @Host() @Optional() public noAnimation?: ApesNoAnimationDirective) {
    super(cdr, noAnimation);
  }

  show(): void {
    if (!this.apesCondition) {
      super.show();
    } else {
      this.onConfirm();
    }
  }

  onCancel(): void {
    this.apesOnCancel.emit();
    super.hide();
  }

  onConfirm(): void {
    this.apesOnConfirm.emit();
    super.hide();
  }



}
